class Solution(object):
    def wordPattern(self, pattern, s):
        """
        :type pattern: str
        :type s: str
        :rtype: bool
        """
        words = s.split()
        if len(pattern) != len(words):
            return False

        char_to_word = {}
        word_to_char = {}
        for char, word in zip(pattern, words):
            if char not in char_to_word:
                char_to_word[char] = word
            else:
                if char_to_word[char] != word:
                    return False
            
            if word not in word_to_char:
                word_to_char[word] = char
            else:
                if word_to_char[word] != char:
                    return False
        return True
if __name__ == '__main__':
    pattern = "abba"
    s = "dog cat cat dog"
    print(Solution().wordPattern(pattern, s))
# 思路：
# 一一映射只需要建立两个字典对应关系进行，即双向对应